<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/11/13 15:22:14 $)
// $RCSfile: loginshare.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* LoginShare Class
*/
class LoginShareCore
{
	var $moduleloaded = false;

	/**
	* Loads up the active login share module
	*/
	function loadModule()
	{
		global $_SWIFT, $session;

		// Is there an already existing module loaded?
		if ($this->moduleloaded)
		{
			return true;
		}

		$_loginshare = $this->getLoginModule();
		$_SWIFT["loginapimodule"] = $this->loginapimoduleid;

		if (empty($_loginshare["include"]))
		{
			$session->updateSession($_SWIFT["session"]["sessionid"], 0);
			return -2;
		}

		// Include the module
		require_once ("./includes/LoginShare/".$_loginshare["include"]);
		loginShareInit();
	}

	/**
	* Gets the login module id and loads it
	*/
	function getLoginModule()
	{
		global $_SWIFT, $template;

		// Get the auth module for this template group
		if (empty($_SWIFT["tgroup"]["loginapi_moduleid"]))
		{
			$this->loginapimoduleid = LOGINAPI_DEFAULT;
		} else {
			$this->loginapimoduleid = $_SWIFT["tgroup"]["loginapi_moduleid"];
		}

		// Now that we have the auth module, we need to use it to fetch the authentication credentials
		require_once ("./includes/LoginShare/loginshare.config.php");

		if (defined("ISMODERNBILL"))
		{
			$this->loginapimoduleid = LOGINAPI_MODERNBILL;
		} else if (defined("ISMODERNBILLV5")) {
			$this->loginapimoduleid = LOGINAPI_MODERNBILLV5;
		}

		$loginapimoduleid = $this->loginapimoduleid;


		$_loginshare = $_LOGINAPI[$loginapimoduleid];
		if (empty($_loginshare["include"]))
		{
			// Seems like the module no longer exists? WTF?
			$_loginshare = $_LOGINAPI[LOGINAPI_DEFAULT];
			$this->loginapimoduleid = LOGINAPI_DEFAULT;
		}

		$template->assign("loginsharemodule", $this->loginapimoduleid);

		return $_loginshare;
	}

	/**
	* Authorize a user
	*/
	function authorize($email, $password)
	{
		global $dbCore, $_SWIFT, $session;

		// Check to see if a session exists
		if (empty($_SWIFT["session"]["sessionid"]))
		{
			return -1;
		} else if (!$this->moduleloaded) {
			return 0;
		}
		
		// Authorize the user
		$authresult = loginShareAuthorize($email, $password);
		if (!$authresult)
		{
			// Login failed
			$session->updateSession($_SWIFT["session"]["sessionid"], 0);
			return 0;
		}

		// Login Success! Is this a custom module?
/*		if ($this->loginapimoduleid != LOGINAPI_DEFAULT)
		{
			// Yes, we need to see if auto insert is required here
		} else {*/
			// Its the default module, simply update the userid for this user and return success
			$session->updateSession($_SWIFT["session"]["sessionid"], loginShareUserID());
			return 1;
/*		}*/
	}

	/**
	* Logout the current user
	*/
	function logout()
	{
		global $dbCore, $_SWIFT;

		if (!$this->moduleloaded)
		{
			return 0;
		}

		if (!empty($_SWIFT["session"]["sessionid"]))
		{
			// Authorize the user

			loginShareLogout();

			return true;
		}

		return false;
	}

	/**
	* Checks to see if usergroupid is empty, if it is then it sets it according to tgroup default usergroup
	*/
	function processUserGroup()
	{
		global $dbCore, $_SWIFT, $template;

		if (empty($_SWIFT["user"]["usergroupid"]) && $_SWIFT["user"]["loggedin"] == false)
		{
			// Its empty and the user is a guest.
			$_SWIFT["user"]["usergroupid"] = $_SWIFT["tgroup"]["guestusergroupid"];
		} else if (empty($_SWIFT["user"]["usergroupid"]) && $_SWIFT["user"]["loggedin"] == true) {
			// Load the registered user group
			$_SWIFT["user"]["usergroupid"] = $_SWIFT["tgroup"]["regusergroupid"];
		}
		
		// ======= !! REMOVE LATER !! =======
		$template->assign("_USER", $_SWIFT["user"]);

		return true;
	}

	/**
	* Loads the current user access masks
	*/
	function loadPermissions()
	{
		global $_SWIFT, $template;

		if (empty($_SWIFT["user"]["usergroupid"]))
		{
			return false;
		} else if (!isset($_SWIFT["usergroupsettingcache"][$_SWIFT["user"]["usergroupid"]])) {
			return false;
		}
		
		$_SWIFT["user"]["permissions"] = $_SWIFT["usergroupsettingcache"][$_SWIFT["user"]["usergroupid"]];

		// This is where we assign the user data to our templates
		$template->assign("permission", $_SWIFT["user"]);

		return true;
	}

	/**
	* Load the SWIFT User
	*/
	function loadSWIFTUser($userid)
	{
		global $dbCore, $_SWIFT;

		// Fetch User Credentials
		$_user = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."users` WHERE `userid` = '". intval($userid) ."';");
		if (empty($_user["userid"]))
		{
			return false;
		}

		// Fetch emails associated with this user
		$_user["email"] = array();
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."useremails` WHERE `userid` = '". intval($_user["userid"]) ."';");
		$firston = false;
		while ($dbCore->nextRecord())
		{
			if (!$firston)
			{
				$_user["primaryemail"] = $dbCore->Record["email"];
				$firston = true;
			}
			$_user["email"][] = $dbCore->Record["email"];
		}

		$_user["isloggedin"] = true;
		$_user["loggedin"] = true;

		return $_user;
	}
}
?>